package com.xzq.erp.controller;


import com.xzq.erp.aspect.ApiOperationLog;
import com.xzq.erp.domain.dto.PageDTO;
import com.xzq.erp.domain.dto.AfterSalesServiceDTO;
import com.xzq.erp.domain.query.AfterSalesServicePageQuery;
import com.xzq.erp.domain.vo.AfterSalesServiceVO;
import com.xzq.erp.service.IAfterSalesServiceService;
import com.xzq.erp.utils.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 售后服务表 前端控制器
 * </p>
 *
 * @author xzq
 * @since 2025-05-16
 */
@RestController
@RequestMapping("/after-sales-service")
@RequiredArgsConstructor
@Tag(name = "售后服务相关接口")
public class AfterSalesServiceController {

    private final IAfterSalesServiceService afterSalesServiceService;

    /**
     * 分页查询售后服务列表
     */
    @GetMapping("/list")
    @Operation(summary = "分页获取售后服务列表")
    @ApiOperationLog(description = "分页获取售后服务列表")
    @PreAuthorize("hasAuthority('after-sales-service:list') || hasRole('ADMIN')")
    public Response<PageDTO<AfterSalesServiceVO>> getAfterSalesServiceList(@ParameterObject @Validated AfterSalesServicePageQuery query) {
        return Response.success(afterSalesServiceService.getAfterSalesServiceList(query));
    }

    /**
     * 新增售后服务
     */
    @PostMapping
    @Operation(summary = "新增售后服务")
    @ApiOperationLog(description = "新增售后服务")
    @PreAuthorize("hasAuthority('after-sales-service:add') || hasRole('ADMIN')")
    public Response<Long> add(@Validated @RequestBody AfterSalesServiceDTO afterSalesServiceDTO) {
        return Response.success(afterSalesServiceService.add(afterSalesServiceDTO));
    }

    /**
     * 修改售后服务记录
     */
    @PutMapping
    @Operation(summary = "修改售后服务记录")
    @ApiOperationLog(description = "修改售后服务记录")
    @PreAuthorize("hasAuthority('after-sales-service:update') || hasRole('ADMIN')")
    public Response<Void> update(@Validated @RequestBody AfterSalesServiceDTO afterSalesServiceDTO) {
        afterSalesServiceService.update(afterSalesServiceDTO);
        return Response.success();
    }

    /**
     * 获取售后服务详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取售后服务详情")
    @ApiOperationLog(description = "获取售后服务详情")
    @PreAuthorize("hasAuthority('after-sales-service:detail') || hasRole('ADMIN')")
    public Response<AfterSalesServiceVO> getDetail(@PathVariable("id") @NotNull Long id) {
        return Response.success(afterSalesServiceService.getDetailById(id));
    }

    /**
     * 更新售后服务状态
     */
    @PutMapping("/status/{id}")
    @Operation(summary = "更新售后服务状态")
    @ApiOperationLog(description = "更新售后服务状态")
    @PreAuthorize("hasAuthority('after-sales-service:update') || hasRole('ADMIN')")
    public Response<Void> updateStatus(
            @PathVariable("id") @NotNull Long id,
            @RequestParam("status") @NotNull Integer status) {
        afterSalesServiceService.updateStatus(id, status);
        return Response.success();
    }

    /**
     * 提交满意度评价
     */
    @PutMapping("/satisfaction/{id}")
    @Operation(summary = "提交满意度评价")
    @ApiOperationLog(description = "提交满意度评价")
    @PreAuthorize("hasAuthority('after-sales-service:update') || hasRole('ADMIN')")
    public Response<Void> submitSatisfaction(
            @PathVariable("id") @NotNull Long id,
            @RequestParam("score") @NotNull Integer score) {
        afterSalesServiceService.submitSatisfactionScore(id, score);
        return Response.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping
    @Operation(summary = "批量删除售后服务记录")
    @ApiOperationLog(description = "批量删除售后服务记录")
    @PreAuthorize("hasAuthority('after-sales-service:delete') || hasRole('ADMIN')")
    public Response<Void> delete(@RequestBody @NotEmpty List<Long> ids) {
        afterSalesServiceService.removeBatchByIds(ids);
        return Response.success();
    }
}
